#!/usr/bin/perl

use DBI;

$SRC_DB=$ENV{'SRC_DB'};
$TGT_DB=$ENV{'TGT_DB'};
sub select_node
{
#	print "select_node.node:$_[0]\n";
	$t_node = $_[0];

	$db = DBI->connect('DBI:ODBC:'.$SRC_DB, 'hubiio', 'hubiio');

$pa01 = @ARGV[0];
$pa02 = @ARGV[1];

#print "---- s_COMPARE_EXPRESSION:$pa01,$pa02<\n";

#$pa01 = S_A2A001;
#$pa02 = R_A1J1_I01;

#$pa01 = S_E2A002;
#$pa02 = R_P3A1_IE1;

#$pa01 = S_A2A002;
#$pa02 = R_FLAG_GRID_I32;
#$pa02 = R_001000_IE2;
#	$pa02 = R_A101_I02;
#	$pa02 = R_A1JY_I32;
#$pa02 = R_A500_I02;
#$pa02 = R_CS53_I02;
#$pa02 = R_SMA1_I02;

#$pa01 = S_A2A001;
#$pa02 = R_A1J1_I01;
#$pa02 = R_CS53_I01;


	$sql01 = "
SELECT 
-- A.APP_NAME, B.MSG_NAME, 
--	C.RULE_NAME,
	D.SEQ_NUM ,
	E.FLD_NAME1 || ' ' ||
	DECODE( F.OPER_SYMBOL , 
			'STRING=', 		'==',
			'STRING<>', 		'!=',
			'STRING_TRIM=', 	'==',
			'STRING_TRIM<>', 	'!=',
		F.OPER_SYMBOL )
	||
	' \"' || D.ARG_VAL || '\"',
	D.BOOLEAN_SEQ_NUM,
	C.ARG_CNT
--	G.BOOLEAN_OPER,
--	G.BOOLEAN_SEQ_NUM,
--	G.PARNT_SEQ_NUM
FROM 	NNR_APP A,
	NNR_MSG_MSTR B,
	NNR_RULE C,
	NNR_ARG D,
	NNR_OPERATION E,
	NNR_OPERATOR F
--	NNR_BOOLEAN G
WHERE 		
	A.APP_NAME = '$pa01'
AND	C.RULE_NAME = '$pa02'
AND	B.MSG_ID = C.MSG_ID
AND 	A.APP_ID = C.APP_ID
AND 	D.APP_ID = C.APP_ID
AND	D.MSG_ID = C.MSG_ID
AND	D.RULE_ID = C.RULE_ID
-- AND E.FLD_NAME1 <> 'STR0001'
AND 	E.APP_ID = C.APP_ID
AND	E.MSG_ID = C.MSG_ID
AND	E.OPERN_ID = D.OPERN_ID
AND	F.OPER_ID = E.OPER_ID
--AND 	G.APP_ID = C.APP_ID
--AND	G.MSG_ID = C.MSG_ID
--AND	G.RULE_ID = C.RULE_ID
";
  	$sth01 = $db->prepare($sql01);

	$sth01->execute();
  
	$i=1;
	while(@data = $sth01 -> fetchrow_array){
#    		print "[$i],$data[0],$data[1],$data[2]";
#    		print ",$data[3],$data[4],$data[5]\n";

		$s_data00[$i] = $data[0];
		$s_data01[$i] = $data[1];
		$s_data02[$i] = $data[2];
		$s_data03[$i] = $data[3];
		$s_data04[$i] = $data[4];
		$s_data05[$i] = $data[5];

    	#	print "|i:$i $s_data00[$i]\n";

		$i += 1;

	}
	$sth01->finish;

	$sql01 = "
SELECT 
--	AA.APP_NAME,
--	AA.RULE_NAME,
	AA.BOOLEAN_SEQ_NUM,
	AA.BOOLEAN_OPER,
	AA.ARG_CNT,
	AA.PARNT_SEQ_NUM,
	MIN(BB.SEQ_NUM),
	MAX(BB.SEQ_NUM)
FROM (
SELECT
A.APP_NAME, 
A.APP_ID, 
	C.MSG_ID,
	C.RULE_NAME,
	C.RULE_ID,
	D.BOOLEAN_SEQ_NUM,

	DECODE( D.BOOLEAN_OPER,
			'&', 		'&&',
			'|', 		'||',
		D.BOOLEAN_OPER) BOOLEAN_OPER,

	D.ARG_CNT,
	D.PARNT_SEQ_NUM
FROM 	NNR_APP A,
	NNR_RULE C,
	NNR_BOOLEAN D
WHERE
	A.APP_NAME = '$pa01'
AND	C.RULE_NAME = '$pa02'
AND 	A.APP_ID = C.APP_ID
AND 	D.APP_ID = C.APP_ID
AND	D.MSG_ID = C.MSG_ID
AND	D.RULE_ID = C.RULE_ID
ORDER BY A.APP_NAME, 
	C.RULE_NAME,
	D.BOOLEAN_SEQ_NUM,
	D.BOOLEAN_OPER,
	D.ARG_CNT,
	D.PARNT_SEQ_NUM
) AA, NNR_ARG BB
WHERE BB.APP_ID 	(+)= AA.APP_ID
AND	BB.MSG_ID 	(+)= AA.MSG_ID
AND	BB.RULE_ID 	(+)= AA.RULE_ID
AND	BB.BOOLEAN_SEQ_NUM (+)= AA.BOOLEAN_SEQ_NUM
GROUP BY AA.BOOLEAN_SEQ_NUM,
	AA.BOOLEAN_OPER,
	AA.ARG_CNT,
	AA.PARNT_SEQ_NUM
ORDER BY AA.BOOLEAN_SEQ_NUM
";
  	$sth01 = $db->prepare($sql01);

	$sth01->execute();
  
	$j=0;
	while(@data = $sth01 -> fetchrow_array){
#    		print "[$j],$data[0],$data[1],$data[2]";
#   		print ",$data[3],$data[4],$data[5]\n";

		$r_data00[$j] = $data[0];
		$r_data01[$j] = $data[1];
		$r_data02[$j] = $data[2];
		$r_data03[$j] = $data[3];
		$r_data04[$j] = $data[4];
		$r_data05[$j] = $data[5];

    	#	print "|i:$i $s_data01[$j]\n";

		$j += 1;

	}
	$sth01->finish;


	$db->disconnect;
}

&select_node;

for( $k = 0; $k < $j; $k++ ) {
	$st_fld = $r_data04[$k];
	$ed_fld = $r_data05[$k];
#	printf "loop ($k) $st_fld ~ $ed_fld , $r_data03[$k]\n";

	# 'PARNT_SEQ_NUM' is grater than 0
	if ( $r_data03[$k] > 0 ) {

		$br_st = $st_fld;
		$br_ed = $ed_fld;

		for( $l = 0; $l <$j; $l ++) {
#			print "		STEP BOOLEAN_SEQ_NUM :";
#			print "	$r_data00[$k] vs ";
#			print "	$r_data03[$l]\n";
			
			if( $r_data00[$k]  == $r_data03[$l] ) {
#				print "-->$r_data04[$l]\n";
				$br_st = $r_data04[$l];
			}
		}

		$s_data01[$br_st] = "($s_data01[$br_st]";
		$s_data01[$br_ed] = "$s_data01[$br_ed])";
	}
#	printf "\n";
}

for( $k = 0; $k < $j; $k++ ) {
	$st_fld = $r_data04[$k];
	$ed_fld = $r_data05[$k];

	# PARNT_SEQ_NUM
#	if ( $r_data03[$k] == 0 ) {
#		$ed_fld = $st_fld + $r_data02[$k] - 1;
#		if( ($st_fld == 1) && ($ed_fld == 1) ) {
#			$ed_fld = 2;
#		}
#	}

#	printf "ARG_CNT : $r_data02[$k] ";
#	printf "$r_data04[$k] ~ $r_data05[$k]\n";
#	if ( $r_data02[$k] > ($r_data05[$k] - $r_data04[$k]) ) {
#		$ed_fld = $ed_fld + 1;
#	}

#	printf "st : $st_fld ~ $ed_fld\n";

	#--------------------------------
#	printf "ARG CNT : $r_data02[$k]\n";
	$arg_cnt = $r_data02[$k];

	#--------------------------------
	$is_pre_post = 1;
	if ( $st_fld == 1 ) {
		$is_pre_post = 0;
	}
	# PARNT_SEQ_NUM
#	if ( $r_data03[$k] > 0 ) {
#		$is_pre_post = 0;
#	}

$str_buf = substr $s_data01[$st_fld], 0, 1;
#printf "AAA substr$s_data01[$st_fld]--->$str_buf\n";
	if ( $str_buf eq '(' ) {
		$is_pre_post = 0;
	}

	if ( $st_fld > 0 ) {
		for( $l = $st_fld; $l <= $ed_fld; $l++ ) {

			# ARG_CNT == 1 && 
			last if (($r_data02[$k] == 1) && ($l == $ed_fld) &&
				($st_fld != $ed_fld) );

			# BOOLEAN_SEQ_NUM
#			printf "--> $r_data00[$k] -- $s_data02[$l]\n";
			if( $r_data00[$k] == $s_data02[$l] ){

				if( $is_pre_post == 1 ) {
					$s_data01[$l] = "$r_data01[$k] $s_data01[$l]";
					$arg_cnt--;
				} else {
#printf "$s_data03[$l]\n";
					if($l == $s_data03[$l]) {
						$s_data01[$l] = "$r_data01[$k] $s_data01[$l]";
					} else {
						$s_data01[$l] = "$s_data01[$l] $r_data01[$k]";
					}
					$arg_cnt--;
				}

			}
			last if ( ($r_data02[$k] > 1) && ($arg_cnt == 1) );
		}
	}
}

for( $k = 0; $k < $j; $k++ ) {
	$st_fld = $r_data04[$k];
	$ed_fld = $r_data05[$k];

	if ( $st_fld eq '') {
#		printf "NULL  \n";
		for( $l = 0; $l <$j; $l ++) {
			if( $r_data00[$k]  == $r_data03[$l] ) {
#				print "-->$r_data04[$l]\n";
				$st_fld = $r_data04[$l];
				$ed_fld = $r_data04[$l];
				$operr = $r_data01[$k];
			}
		}
	}
}

$s_data01[$st_fld] = "$operr $s_data01[$st_fld]";
#printf "STEP02    $s_data01[$st_fld]\n";
#printf "STEP02    <- $operr\n";

$fld_cnt = $i;

#printf "$pa01:$pa02:";

$REQ = @ARGV[2];
#printf "------ REQ : $REQ<\n";

for( $k = 0; $k < $fld_cnt; $k++ ) {
#$yy=substr $s_data01[$k],0,9;
#print ">>$yy<<\n";
#chop $s_data01[$k];
#print ">$s_data01[$k]<>$tmp_str<\n";
#	printf ">>>$s_data01[$k]<<<\n";
	
	$s_data01[$k] =~ s/^ //g;
#	printf "--->>>$s_data01[$k]<<<\n";
	if($REQ eq 'Y') {
		if( 	
			((substr $s_data01[$k],0,7) ne 'STR0001') && 
			((substr $s_data01[$k],0,9) ne 'STR0016_1') &&
			((substr $s_data01[$k],0,9) ne 'STR0016_2') &&
			((substr $s_data01[$k],0,7) ne 'STR0015') &&
			((substr $s_data01[$k],0,8) ne '(STR0015') 
									) {

			printf "$s_data01[$k] ";
		}
	} elsif($REQ eq 'N') {
		if(	
			((substr $s_data01[$k],0,7) ne 'STR0001') && 
			((substr $s_data01[$k],0,9) ne 'STR0016_3') &&
			((substr $s_data01[$k],0,7) ne 'STR0015') &&
			((substr $s_data01[$k],0,8) ne '(STR0015') 
									) {

			printf "$s_data01[$k] ";
		}
	} else {
		if(	
			((substr $s_data01[$k],0,7) ne 'STR0001') && 
			((substr $s_data01[$k],0,7) ne 'STR0015') &&
			((substr $s_data01[$k],0,8) ne '(STR0015') 
									){
			printf "$s_data01[$k] ";
		}
	}

}
printf "\r\n";
